<?php
/* ***************************************
 * Copyright Reserved @ chinaaus       **
 * @Procedure: Coodinator of the web **
 * @Designer: Cavin Zhang           **
 * @Date: 24 July, 2011           **
 * *****************************/

	/*
	 * Clear the cache within posted search result page
	 * Need to be placed in front of the session start
	 */
session_cache_limiter("private,must-revalidate");

header('Content-Type:text/html;charset=gbk');
header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
@session_start();

/*
 * Close the warning caused by duplicate usage of the session
 */
ini_set('session.bug_compat_warn', 0);
ini_set('session.bug_compat_42', 0);
ini_set('session.gc_maxlifetime',3600);
//setcookie(session_name(), session_id(), time() + $lifeTime, "/");
if($isDevelopmentMode == 'ON')
{
	ini_set("display_errors", "On"); 
	ini_set("display_startup_errors","On");
}

include '../classes/functionlist.php';
include '../classes/class.CsDatabaseOpera.php';
include '../classes/globalvar.php';
include '../classes/os.constants.inc.php';
include '../phplib/php/template.inc';
include '../classes/class.InputFormType.php';
include '../classes/class.pageNaviDone.php';
//include '../classes/IndexDoc.php';
include '../classes/class.merchantMemberSpace.php';
include '../classes/class.overseaFormConfigured.php';
include '../classes/class.errorEventLog.php';
include '../classes/class.overseaMerchantUser.php';
include '../classes/class.compassSNSTriggerCollection.php';

if (OVERSEA_LANG == 1)
{
	include '../templates/lang/eng/main.php';
}
else
{
	include '../templates/lang/chn/main.php';
}

if ( !ini_get('register_globals') ) 
{ 
    extract($_POST); 
    extract($_GET); 
    extract($_SERVER); 
    extract($_FILES); 
    extract($_ENV); 
    extract($_COOKIE); 
    
    if ( isset($_SESSION) ) 
    { 
        extract($_SESSION); 
    } 
} 


//declare an object of template
$zTemplate = new Template ();
if(isset($_GET['fo']))
{
	$dbObj = new CsDatabaseOpera ($dbHostName, $dbUserName, $dbPswd,$dbName);
	
	/*
	 * Get City list
	 * Initialize the city array OF Chinese&English
	 */
	if(!$_SESSION['cityArrayEng'] || !$_SESSION['cityArrayChs'])
	{
		//echo '111';
		session_register('cityArrayEng');
		session_register('cityArrayChs');
		$sql = 'SELECT * FROM `cs_city_list` ORDER BY `city_id` ASC';
		$rs = $dbObj -> executeQuery($sql);
		
		foreach ($rs as $val)
		{
			$cityArrayEng[$val['city_id']] = $val['name_eng'];
			$cityArrayChs[$val['city_id']] = $val['name_chs'];
		}
		
	}
	
	/*Check if the user can log on automatically*/
	if(!isset($aususrpinverid) && !isset($aususrtype))
	{
		if(isset($_COOKIE['memberAccountName'])&& isset($_COOKIE['memberAccountPswd']))
		{
			//errorEventLog::displaysSQLPhase('Check remembered password before -> automatically log on!');
			//alertMsg(FALSE,'3',WELCOME_COMPASS_HOME.'/login.html?autologin');
			for($i=0; $i<3; $i++)
			{
				$tableName = $tableSelectionArr[$i];
				//blocked users can't login
				$sql = "SELECT * FROM `".$tableName."` a INNER JOIN ".$tableMappingArr[$tableName]." b ON a.temp_userid=b.temp_userid WHERE `emailadd` = '".mysql_real_escape_string($_COOKIE['memberAccountName'])."' AND 
								`user_pswd` = '".mysql_real_escape_string($_COOKIE['memberAccountPswd'])."' AND `acc_status` <> 10  limit 0,1";
				$result = $dbObj -> getOneQuery($sql);
				if($result) 
				{
					if($i > '0')
					{
						$sel = $result['org_type'];
					}
					else
					{
						$sel = '0';
					}
					
					$_SESSION['aususrrealid'] = $result['usr_id'];
					$_SESSION['aususrlastlogin'] = $result['last_log_in'];
					$_SESSION['aususrpinverid'] = $result['temp_userid'];
					$_SESSION['aususrname'] = $_COOKIE['memberAccountName'];
					$_SESSION['aususrtype'] = $sel;	
					$_SESSION['logstatus'] = $result['acc_status'];
					$_SESSION['myCityChoice'] = $result['city_id'];
					extract($_SESSION);
					break;
				}
			}
		
			if(!isset($sel))
			{
				/*valid days last for one week*/
				setcookie('memberAccountName','',time()-3600*24*7);
				setcookie('memberAccountPswd','',time()-3600*24*7);
				errorEventLog::displaysSQLPhase('PASS doesnt fit therefore clean the cookies!');
			}
		}
	}
	//Added by Liang Fan, if city is attached, still goto home page
	if(isset($_GET['defcity']))
	{
		$citySeleName = $_GET['defcity'];
		if(in_array($citySeleName,$cityArrayEng))
		{	
			$citySeleID = array_search($citySeleName,$cityArrayEng); /*Get the city ID*/
			$_SESSION['myCityChoice']=$citySeleID;
		}
		else
		{
			$_SESSION['myCityChoice']=9; //Australia
		}
		header("location:".WELCOME_COMPASS_HOME."/index.html");
		exit;
	}
	/*
	* if the page location is index, firstly verify if 
	* any city was selected b4
	*/
	if(!isset($_GET['defcity'])) /*if the URL is clean*/
	{
				if(!isset($_SESSION['myCityChoice'])) /*the beginning of opening our web ?*/
				{
					//errorEventLog::displaysSQLPhase('The beginning of opening our web! No city');
					if(isset($_COOKIE['iCity']))
					{
						//errorEventLog::displaysSQLPhase('Successfully Get the last visited record,cityid='.$_COOKIE['iCity']);
						$my_love_city =  $_COOKIE['iCity'];
						@session_register('myCityChoice');
						$_SESSION['myCityChoice'] = $my_love_city;
						//alertMsg(FALSE,'3',WELCOME_COMPASS_HOME.'/'.getCityName($my_love_city, '1'));
						//exit;
					}
				}
				else 
				{
					$my_love_city = $_SESSION['myCityChoice']; /*Has the session recorded*/
				}
	 }
	 else  /*Has the city name within the URL*/
	 {
				$citySeleName = $_GET['defcity'];
				/*find the city*/
				if(in_array($citySeleName,$cityArrayEng))
				{	
					$citySeleID = array_search($citySeleName,$cityArrayEng); /*Get the city ID*/
					if($_SESSION['myCityChoice']!=$citySeleID) /*if the city defers the old one*/
					{
						//errorEventLog::displaysSQLPhase('Reregister the session of the city,previous is '.$_SESSION['myCityChoice'].' ; while the new is '.$citySeleID);
						@session_unregister('myCityChoice');
						@session_register('myCityChoice');
						$_SESSION['myCityChoice'] = $citySeleID;
					//	errorEventLog::displaysSQLPhase('Reregister the Cookies of the city,previous is '.$_COOKIE['iCity']);
						setcookie('iCity','',time()-3600*24*7,'/');
						setcookie('iCity',$citySeleID,time()+3600*24,'/');	
						//errorEventLog::displaysSQLPhase('Reregister the Cookies of the city,current is '.$_COOKIE['iCity']);
					}
				}
				else /*Can not found in the array*/
				{
					header('location:'.ERROR_404);
					exit;
				}	
	  }
	
	
	switch($fn)
	{
		case 'index':
			if (isset($my_love_city))
			{
				//header('location:'.WELCOME_COMPASS_HOME.'/'.getCityName($my_love_city, '1'));
				alertMsg(FALSE,'3',WELCOME_COMPASS_HOME.'/'.getCityName($my_love_city, '1'));
				exit;
			}
			else
			{
				include '../'.$fo.'/'.$fn.'.php';
			}
		break;
		default:
			/* identify if it is pointed to the DB operation*/
			if(isset($opt))
			{
				$fn = $fn.'_exec';
			}
			elseif(isset($opg))
			{
				$fn = $fn.'_'.$opg;
			}
			//echo '../'.$fo.'/'.$fn.'.php';
			//exit ;
			if(file_exists('../'.$fo.'/'.$fn.'.php'))
				include '../'.$fo.'/'.$fn.'.php';
			else 
				header('location:'.ERROR_404);
			break;
	}
}
else
{
	header('location:'.ERROR_404);
	exit;
}

include "../main/footer.php";
include "../main/header.php";
$zTemplate -> set_var('footer',$footer);
$zTemplate -> set_var('Header',$header);
$zTemplate -> set_var($currentPage,$current_location);


if(is_array($langPackage))
{
	foreach ($langPackage as $val)
	{
		$zTemplate -> set_var($val,$MStr[$val]);
	}
}
$zTemplate->set_var ( 'HTMLHeader', $MStr['sHead'] );
$zTemplate->set_var ( 'domainPath', WELCOME_COMPASS_HOME );
$zTemplate->set_var ( 'sGlobalTitle', $MStr['globalslogan'] );
/**
 * Some configuration
 */
$zTemplate->set_var(array('commsEachPage'=>COMMS_EACH_PAGE));
if (isset($_SESSION['aususrpinverid']))
{
	$zTemplate->set_var ('Login2Disapear', "display:none");
}
else
{
	$zTemplate->set_var ('Login2Display', "display:none");
}
//PRINT THE RESULT
$zTemplate->parse ( 'mains', 'main' );
$zTemplate->p ( 'mains' );

?>